﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace hyjiacan.util.web.wp
{
    internal static class Settings
    {
        /// <summary>
        /// 配置文件名称
        /// </summary>
        private static string file;
        private const string section = "mysql";

        static Settings()
        {
            string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "hyjiacan\\wordpressadmin");
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            file = Path.Combine(path, "server.ini");
        }

        /// <summary>
        /// 配置项名称
        /// </summary>
        public enum Items
        {
            /// <summary>
            /// 数据库服务器地址
            /// </summary>
            Server,
            /// <summary>
            /// 登录数据库用户名
            /// </summary>
            Username,
            /// <summary>
            /// 登录数据库密码
            /// </summary>
            Password,
            /// <summary>
            /// 登录的数据库
            /// </summary>
            Database,
            /// <summary>
            /// 表前缀
            /// </summary>
            Prefix
        }

        /// <summary>
        /// 读配置
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public static string Get(Items item)
        {
            StringBuilder data = new StringBuilder(1024);
            int length = NativeMethods.GetPrivateProfileString(section, item.ToString(), null, data, 1024, file);
            return data.ToString();
        }

        /// <summary>
        /// 写配置
        /// </summary>
        /// <param name="Items"></param>
        /// <param name="value"></param>
        public static void Set(Items item, string value)
        {
            NativeMethods.WritePrivateProfileString(section, item.ToString(), value, file);
        }
    }
}
